package com.thalesgroup.preferenceFramework.models.LCPnet.impl;

import junit.framework.TestCase;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.junit.Before;
import org.junit.Test;

import com.thalesgroup.preferenceFramework.models.LCPnet.LCPnet;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetEvaluationException;

public class LCPNetImplTests extends TestCase {
	private LCPnet lcpnet_test;
	private LCPnet lcpnet_test_isAcyclic;
	private LCPnet lcpnet_test_outOfDomainQoS;
	
	@Before
	public void setUp() throws Exception {
		lcpnet_test = LCPnetImpl.load("tests/com/thalesgroup/preferenceFramework/models/LCPnet/impl/lcpnet_test.lcpnet");
		assertNotNull(lcpnet_test);
		
		lcpnet_test_isAcyclic = LCPnetImpl.load("tests/com/thalesgroup/preferenceFramework/models/LCPnet/impl/lcpnet_test_isAcyclic.lcpnet");
		assertNotNull(lcpnet_test_isAcyclic);
		
		lcpnet_test_outOfDomainQoS = LCPnetImpl.load("tests/com/thalesgroup/preferenceFramework/models/LCPnet/impl/lcpnet_test_outOfDomainQoS.lcpnet");
		assertNotNull(lcpnet_test_outOfDomainQoS);
	}

	@Test
	public void testIsAcyclic() throws LCPnetEvaluationException {
		assertTrue(lcpnet_test_isAcyclic.isAcyclic());
	}

	@Test
	public void testGetUtility() throws LCPnetEvaluationException {
		EList<String> nodeNames = new BasicEList<String>();
		nodeNames.add("S");
		nodeNames.add("B");
		nodeNames.add("R");
		
		EList<Double> nodeValues = new BasicEList<Double>();
		nodeValues.add(0.4);
		nodeValues.add(1.0);
		nodeValues.add(0.51);
		
		double utility = lcpnet_test.getUtility(nodeNames, nodeValues);
		assertEquals(0.7072621253417823, utility);
	}
	
	@Test
	public void testMultipleEvaluations() throws LCPnetEvaluationException {
		for(int i=0;i<10;i++) {
			testGetUtility();
		}
	}
	
	@Test
	public void testOutOfDomainQoSValue() throws LCPnetEvaluationException {
		//1 - On lance une premiere evaluation correcte (une regle declanche dans le FIS)
		EList<String> nodeNames = new BasicEList<String>();
		nodeNames.add("A");
		
		EList<Double> nodeValues = new BasicEList<Double>();
		nodeValues.add(0.7);
		
		double utility = lcpnet_test_outOfDomainQoS.getUtility(nodeNames, nodeValues);
		
		//2 - On lance une seconde evaluation incorrecte (aucune regle ne declanche)
		nodeValues = new BasicEList<Double>();
		nodeValues.add(0.3);
		
		utility = lcpnet_test_outOfDomainQoS.getUtility(nodeNames, nodeValues);
		//On constate alors bien une valeur d'indecision (0.5) en retour
		assertEquals(0.5, utility);
	}
}
